<template>
  <section class="component modify-dataDict">
    <el-form :rules="rules" :model="changeParams" ref="add-form" label-width="130px" >
            <el-form-item label="父类别" prop="parentLevel">
                <el-input v-model="changeParams.parentLevel" size="small" disabled></el-input>
            </el-form-item>
            <el-form-item label="字段名称" prop="name">
                <el-input v-model="changeParams.name" size="small"></el-input>
            </el-form-item>
            <el-form-item label="状态" min-width="140px" prop="dataStatus">
                <el-select v-model="changeParams.dataStatus" placeholder="请选择" size="small">
                  <el-option label="有效" value="EFFECTIVE"></el-option>
                  <el-option label="无效" value="INVALID"></el-option>
                    <!-- <el-option v-for="{code,name,index} in $dict.getDictData('CommonFlag')" :key="index" :label="name" :value="code">
                    </el-option> -->
                </el-select>
            </el-form-item>
                <el-form-item label="优先级" min-width="140px" prop="level">
                    <el-input v-model="changeParams.level" size="small"></el-input>
                </el-form-item>
    </el-form>
    <div class="operate-buttons">
        <el-button @click="changeCancel" plain>取消</el-button>
        <el-button @click="changeCommit" type="primary">保存</el-button>
    </div>
  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Dependencies } from '~/core/decorator';
import { DataPoolService } from "~/services/common-service/data-pool.service";
import { Emit, Prop, Watch } from 'vue-property-decorator';
import { Getter,Action } from 'vuex-class';


@Component({
  components: {},
})
export default class ModifyDataDict extends Vue {
  @Action public getCustomDataDict
  @Dependencies(DataPoolService)
  private dataPoolService: DataPoolService;

  // 数据实体
  private changeParams: any = {
        parentId: '',
        name: '',
        dataStatus: '',
        level: '',
        parentLevel:''
    };
  private rules: any = {
        parentId:{ required: true},
        name:{ required: true},
        dataStatus: { required: true,message: '请选择状态'},
        level:{required: true,message: '请输入优先级' }
    };
  @Emit('refreshList')
  private refreshList() {}

  @Emit('close')
  private close() {
    this.reset()
  }

  private refresh(data,node){
    this.changeParams.parentLevel=node.parent.label
    this.changeParams.parentId=node.data.parentId
    this.changeParams.name=data.name
    this.changeParams.dataStatus=data.dataStatus
    this.changeParams.level=data.level
    this.changeParams.id=data.id
    this.changeParams.dataType=data.dataType
  }

  /**
   * 重置form
   */
  private reset() {
    const Form: any = this.$refs["add-form"];
    Form.resetFields();
  }

  /**
   * 提交验证并并与数据库交互
   */
  private changeCommit() {
    const form: any = this.$refs['add-form'];
    form.validate((valid) => {
      if (!valid) {
        return;
      }
      console.log(this.changeParams)
        this.dataPoolService.addDataPoolDict(this.changeParams).subscribe(data => {
        this.reset();
        this.getCustomDataDict();
        this.$emit("close");
        this.$message.success("修改成功!");
        //   刷新el-tree
        this.$emit("refreshList");
        //刷新dataBox
        this.$emit("refreshDataBox");
      });
    });
  }

  private changeCancel(){
    const form:any=this.$refs['add-form']
    form.resetFields();
    this.$emit('close')
  }

  /**
   * 新增公司
   */
//   private addCompany(): boolean {
//     this.companyService.createCompany(this.companyModel).subscribe(
//       (data) => {
//         this.$message.success('新增成功!');
//         this.refreshList();
//       },
//       ({ msg }) => {
//         return false;
//       },
//     );
//     return true;
//   }

  /**
   * 修改公司
   */
//   private updateCompany(): boolean {
//     this.companyService.modify(this.companyModel).subscribe(
//       (data) => {
//         this.$message.success('修改成功!')
//         this.refreshList();
//       },
//       (err) => {
//         return false;
//       },
//     );
//     return true;
//   }
}
</script>

<style lang="less" scoped>
.component.modify-dataDict{
    .changeButton{
      display: flex;
      justify-content: flex-end;
    }
}
</style>
